package com.example.demo.io;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class BioInstance {


}

/**
 * block io Test
 * <p>
 * 阻塞io回顾和练习
 */
class Test {

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(999);
            System.out.println("开启服务端，等待连接中");
            Socket accept = serverSocket.accept();
            System.out.println("已经有设备连接");
            handler(accept);
        } catch (IOException e) {
            e.printStackTrace();
        }

        ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>();

        // 自增加线程安全
        AtomicInteger atomicInteger = new AtomicInteger();
        atomicInteger.getAndIncrement();// 进行自增
    }

    private static void handler(Socket socket) throws IOException {
        // 创建字节数组集合
        byte[] bytes = new byte[1024];
        System.out.println("准备read");

        int read = socket.getInputStream().read(bytes);
        System.out.println("读取完毕");

        if (read != -1) {
            // 打印读取到的值
            String s = new String(bytes, 0, read);
            System.out.println("接收到的值：" + s);
        }
    }
}
